package primary.array;

/**
 * @Author Elephas
 * @Date 2022/1/31
 **/
@FunctionalInterface
public interface IsValidSudoku {
    boolean isValidSudoku(char[][] board);
}
class IsValidSudokuImpl1 implements IsValidSudoku{
    /**
     * 每一行1-9只能出现一次
     * 每一列的1-9只能出现一次
     * 每一个3 x 3 区域内 1-9 只能出现一次
     * 如果满足上述三个条件返回true，否则返回false
     * @param board
     * @return
     */
    @Override
    public boolean isValidSudoku(char[][] board) {
        int[][] rows = new int[9][9];
        int[][] columns = new int[9][9];
        int[][][] subBoxes = new int[3][3][9];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c = board[i][j];
                if(!".".equals(c)){
                    int index = (c - '0') - 1;
                    rows[i][index]++;
                    columns[j][index]++;
                    subBoxes[i / 3][j / 3][index]++;
                    if(rows[i][index] > 1 || columns[j][index] > 1 || subBoxes[i / 3][j / 3][index] > 1){
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
